<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>修改轮播图</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/js/layui/css/layui.css" media="all">
</head>
<style>
    .edui-editor{
        width: 100% !important;
    }
</style>
<body style="height: 750px;background-color: white">
<div style="width:80%;height:100%;margin:15px auto;overflow:auto;" id="aaa">
    <form class="layui-form" action="">
        <input type="hidden" class="input-text" id="id" name="id">
        <div class="layui-form-item" id="title_div">
            <label class="layui-form-label" >标题</label>
            <div class="layui-input-block">
                <input type="text" name="title" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item" id="message_div">
            <label class="layui-form-label" >描述</label>
            <div class="layui-input-block">
                <input type="text" name="message" required  lay-verify="required" placeholder="请输入描述" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">类型</label>
            <div class="layui-input-block">
                <input type="radio" lay-filter="type" id="tu" name="type" value="0" title="普通图片" checked=""><div class="layui-unselect layui-form-radio layui-form-radioed"><i class="layui-anim layui-icon"></i><span >普通图片</span></div>
                <input type="radio" lay-filter="type" id="lian" name="type" value="1" title="超链接"><div class="layui-unselect layui-form-radio"><i class="layui-anim layui-icon"></i><span >超链接</span></div>
            </div>
        </div>

        <div class="layui-form-item" id="aa" style="display: none">
            <label class="layui-form-label" >超链接</label>
            <div class="layui-input-block">
                <input type="text" name="url" id="url"  placeholder="请输入超链接网址" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">顺序权重</label>
            <div class="layui-input-block">
                <input name="sort" type="number" min="0" max="999999999" placeholder="顺序权重，只能输入正整数" id="sort" required lay-verify="sort" placeholder="商品权重数字" autocomplete="off" class="layui-input" onkeyup="value=value.replace(/^(0+)|[^\d]+/g,'')">
            </div>
        </div>
        <!-- 图片上传 -->
        <div class="layui-upload">
            <button type="button" class="layui-btn" id="test" style="display: inline;float: left;" >上传图片</button>
            <div class="layui-upload-list" style="display: inline;margin-left: 20px;">
                <img class="layui-upload-img" name="logo" id="logo" style="width: 360px;height: 200px">
                <p id="demoText"></p>
            </div>
            <span style="font-size: 14px;color: red;margin-left: 110px" >建议图片的高宽比为 1 : 1.8，例如200*360 </span>
            <input type="hidden" class="input-text" id="picture" name="picture">
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" >状态</label>
            <div class="layui-input-block">
                <input type="radio" name="status" id="qiyong" value="1" title="启用" checked=""><div class="layui-unselect layui-form-radio layui-form-radioed"><i class="layui-anim layui-icon"></i><span >启用</span></div>
                <input type="radio" name="status" id="jinyong" value="0" title="禁用"><div class="layui-unselect layui-form-radio"><i class="layui-anim layui-icon"></i><span >禁用</span></div>
            </div>
        </div>

        <div class="layui-form-item" style="margin-top: 20px">
            <div class="layui-input-block">
                <button class="layui-btn" id="addWuye" lay-submit lay-filter="addWuye" >立即修改</button>
                <button type="reset" id="reset" class="layui-btn layui-btn-primary" >重置</button>
            </div>
        </div>
    </form>
</div>
<script src="/js/layui/layui.js"></script>
<script src="/js/layui/layui.all.js"></script>
<script src="/admin/js/token.js"></script>

<script>
    //url 传参
    function GetQueryString(name){
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var a = decodeURI(window.location.search);
        var r = a.substr(1).match(reg);//search,查询？后面的参数，并匹配正则
        if(r!=null)return  unescape(r[2]); return null;
    }
    var form = layui.form;
    var type;//轮播图类型（0：普通  1：链接）
    var $ = layui.jquery;
    var data;

    window.onload = function(){
        layer.load(2);
        data = layui.data("lunbotudata").data;
        $("input[name='id']").val(data.id);
        $("input[name='title']").val(data.title);
        $("input[name='message']").val(data.message);
        $("input[name='picture']").val(data.picture);
        $("input[name='url']").val(data.url);
        $("input[name='sort']").val(data.sort);
        $('#logo').attr('src', "/"+data.picture);
        $.ajax({
            url:'/lunbotu/selectLunbotuID?id='+data.id,
            syne:true,
            type:'get',
            success: function (res) {
                var x = res.data.status;
                if(x==1){
                    $("#qiyong").prop("checked",true);
                }else{
                    $("#jinyong").prop("checked",true);
                }
                form.render();
            }
        });

        type = data.type;
        if(type==1){
            $("#aa").css("display","block");
            $("#lian").prop("checked",true);
        }else{
            $("#aa").css("display","none");
            $("#tu").prop("checked",true);
        }
        form.render();
        layer.closeAll();
    };

    form.on('radio(type)', function (data) {
        // console.log( data );　　   //打印当前选择的信息
        var value = data.value;
        type = value;
        if(value==1){//判断当前多选框是选中还是取消选中
            $("#aa").css("display","block");
        }else{
            $("#aa").css("display","none");
            $("#url").val("");
        }
    });

    layui.use(['form' , 'jquery' , 'upload'], function(){
        var form = layui.form;
        var upload = layui.upload;

        //修改操作
        form.on('submit(addWuye)',function(data){
            var bg = $("input[name='picture']").val();
            var sort = $("input[name='sort']").val();
            if(bg==null || bg==""){
                layer.msg('请上传图片！');
            }else if(sort=='' || sort==null){
                layer.msg("请输入顺序权重");
            }else {
                data.field.images=bg;
                $("#addWuye").prop("disabled",true);
                $("#addWuye").attr("class","layui-btn layui-btn-disabled");
                $.ajax({
                    url:"/lunbotu/updateLunbotu"
                    ,type:"POST"
                    ,contentType: 'application/json'
                    ,data:JSON.stringify(data.field)
                    ,success:function (data) {
                        if(data.code==3){
                            layer.msg('请上传图片！');
                        }else if(data.code==2) {
                            layer.msg('标题已存在！请重新输入');
                        }else if(data.code==0){
                            layer.close(layer.index);
                            layer.msg('修改成功');
                            setTimeout(function(){
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);//关闭弹出层
                                parent.location.reload();//重新加载父页面表格
                            }, 2000);
                        }else if(data.code==1){
                            layer.msg('修改失败！');
                        }else{
                            layer.msg('拒绝访问');
                        }
                    }
                    ,error:function () {
                        console.log("ajax error");
                    }
                });
            }
            return false;
        });

        //bg上传
        var uploadInst = upload.render({
            elem: '#test'
            , url: '/admin/upload/uploadimg' //改成您自己的上传接口
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#logo').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //判断上传是否成功
                if (res.code == 0) {
                    $("#picture").val(res.data);
                    return layer.msg('上传成功！');
                }else{
                    return layer.msg('上传失败！');
                }
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });
        // 设置下拉列表的高度
        $(".layui-form-select dl").css("max-height",'400px')
    });

</script>

</body>
</html>